package com.wang.example.netty.codec.custom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.CharsetUtil;

import java.util.List;

/**
 * @author suzhe
 * @date 2019/1/27
 */
public class JsonDecoder extends MessageToMessageDecoder<ByteBuf>{

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
        String msgStr = in.toString(CharsetUtil.UTF_8);
        System.out.println("JsonDecoder:"+msgStr);
        //将json字符串转为对象
        User user = JSON.parseObject(msgStr,new TypeReference<User>(){});
        //将对象放到list中
        out.add(user);
    }

}
